package com.jd.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.jd.po.User;
import com.jd.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginByPortalAction extends ActionSupport {
	private String username;
	private String password;
	private String group;
	private UserService userService;
	private String tOrb;
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String execute() throws Exception {
		User user = new User();
		User queryUser;
		user.setUsername(username);
		Map session = ActionContext.getContext().getSession();
		if(password==null||password.equals("")){
			password = ((User)session.get("user")).getPassword();
		}
		user.setPassword(password);
		//获得request
		HttpServletRequest request = ServletActionContext.getRequest();
		queryUser = userService.loginUser(user);
		if(queryUser!=null) {
			request.setAttribute("url", "user/SelectProjectByPortal.jsp?tOrb="+getTOrb());
			request.setAttribute("info", "登陆成功");
			//将username保存到session范围中
//			HttpSession session =request.getSession();
//			session.setAttribute("username", username);	
			queryUser.setUserGroup(group);
			session.put("user", queryUser);
			return SUCCESS;
		} else {
			request.setAttribute("url", "loginPortal.jsp");
			request.setAttribute("info", "登陆失败");
			return ERROR;
		}
	}

	public void setTOrb(String tOrb) {
		this.tOrb = tOrb;
	}

	public String getTOrb() {
		return tOrb;
	}

	public void setGroup(String group) {
		this.group = group;
	}

	public String getGroup() {
		return group;
	}

}
